package com.nvyuan.controller;

import com.nvyuan.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@org.springframework.stereotype.Controller
public class Controller {

    @RequestMapping("/users/user")
    public void getUserById() {
        System.err.println("请求进行处理了");
    }

    /**
     * 获取参数的方式 1,以前是通过 httpservletrequest
     */
    @RequestMapping("/users/user1")
    public void getUserById(HttpServletRequest request) {
        System.err.println("username="+request.getParameter("username"));
    }

    @RequestMapping("/users/user2")
    public void getParams2(String username, @RequestParam(name = "pwd",required = false) String password, @RequestParam(defaultValue = "1") int age) {
        System.err.println(username + password + age);
    }


    @RequestMapping("/users/user3")
    public void getParams3(User user) {
        System.err.println(user);
    }


    @RequestMapping("/users/user4")
    public void getParams4(Date birthday) {
        System.err.println(birthday);
    }

    /**
     * 可以通过注解来修改这个格式@DateTimeFormat
     * @param birthday
     */
    @RequestMapping("/users/user5")
    public void getParams5(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birthday) {
        System.err.println(birthday);
    }


    @RequestMapping("/users/user6/{name}")
    public void getParams5(@PathVariable String name) {
        System.err.println(name);
    }


    @RequestMapping("/users/user6/{age}/{name}")
    public void getParams5(@PathVariable  String name,@PathVariable  int age) {
        System.err.println("name =" + name+"age="+age);
    }



    @RequestMapping("/users/user7")
    public void jsonParam(@RequestBody User user) {
        System.err.println(user);
    }
}
